import { Base } from 'base.js';
class Address extends Base {
    constructor() {
        super();
    }

    /** 
     *拼接收货地址
     */
    setAddressInfo(res) {
        var province = res.provinceName || res.province,
            city = res.cityName || res.city,
            country = res.countyName || res.country,
            detail = res.detailInfo || res.detail;
        var totalDetail = city + country + detail;

         //直辖市，取出省部分
         if(!this.isCenterCity(province)) {
            totalDetail=province+totalDetail;
        };
        return totalDetail;
    }

    getAddress(callBack){
        var that = this;
        var params = {
            url: 'address/get',
            sCallBack:function(res){
                if(res){
                    res.data.totalDetail = that.setAddressInfo(res.data);
                    callBack && callBack(res.data) 
                }
            }
        }
        this.request(params);
    }

     /*是否为直辖市*/
     isCenterCity(name) {
        var centerCitys=['北京市','天津市','上海市','重庆市'],
            flag=centerCitys.indexOf(name) >= 0;
        return flag;
    }

    /*更新保存地址*/
    submitAddress(data,callback,eCallBack){
        data = this._setUpAddress(data);
        var param={
            url: 'address/add',
            type:'post',
            data:data,
            sCallBack:function(res){ callback && callback(res) },
            eCallBack:function(err){ eCallBack && eCallBack(err.data)}
        };
        this.request(param);
    }

     /*保存地址*/
     _setUpAddress(res){
        var formData={
                name:res.userName,
                province:res.provinceName,
                city:res.cityName,
                country:res.countyName,
                mobile:res.telNumber,
                detail:res.detailInfo
            };
        return formData;
    }

}

export { Address };